package com.tuju.jetpackfirstdemo.widget;

import android.animation.Animator;
import android.animation.ObjectAnimator;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.BitmapShader;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.LinearGradient;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Shader;
import android.util.AttributeSet;
import android.view.View;


import com.tuju.jetpackfirstdemo.R;
import com.tuju.jetpackfirstdemo.utils.DensityUtil;

import static android.graphics.Paint.ANTI_ALIAS_FLAG;

public class CircleScanView extends View {

    private ObjectAnimator animator;

    public CircleScanView(Context context) {
        super(context);
        initPaint();
    }

    public CircleScanView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initPaint();
    }

    public CircleScanView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initPaint();
    }

    private Paint mCircleParint;
    private Paint mSquePaint;
    private Paint mEdgePaint;

    private void initPaint() {
        mSquePaint = new Paint();
        mSquePaint.setStyle(Paint.Style.FILL);

        mCircleParint = new Paint(ANTI_ALIAS_FLAG);
        mCircleParint.setStyle(Paint.Style.STROKE);

        mEdgePaint = new Paint();
        mEdgePaint.setStyle(Paint.Style.FILL_AND_STROKE);
        mEdgePaint.setStrokeWidth(DensityUtil.dip2px(4));
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        Shader squeShader = new LinearGradient(0f, 0f, 0f, getHeight(), Color.parseColor("#2F3D5F"),
                Color.parseColor("#253048"), Shader.TileMode.CLAMP);
        mSquePaint.setShader(squeShader);

        mCircleParint.setStrokeWidth(20);
        Shader linearShader = new LinearGradient(0f, (getHeight() / 2), getWidth(), (getHeight() / 2), Color.parseColor("#43DE95"),
                Color.parseColor("#24BF92"), Shader.TileMode.CLAMP);
        mCircleParint.setShader(linearShader);

        Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.mipmap.line_scan);
        Shader shader = new BitmapShader(bitmap, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);
        mEdgePaint.setShader(shader);

        Path path = new Path();
        float circleRadiu = Math.min((getWidth() / 2), getHeight() / 2) - DensityUtil.dip2px(20);//原要离周围有些距离
        path.addCircle(getWidth() / 2, getHeight() / 2, circleRadiu, Path.Direction.CW);
        path.addRect(0, 0, getWidth(), getHeight(), Path.Direction.CW);
        path.setFillType(Path.FillType.EVEN_ODD);

        canvas.drawPath(path, mSquePaint);
        canvas.drawCircle(getWidth() / 2, getHeight() / 2, circleRadiu - 10, mCircleParint);
        if (mStartAnimator) {
            canvas.drawLine(0, scanLineHeight, getWidth(), scanLineHeight, mEdgePaint);
        }
//        canvas.drawBitmap(BitmapFactory.decodeResource(getResources(), R.mipmap.line_scan),0,scanLineHeight,mEdgePaint);
    }

    private int scanLineHeight = 0;
    private boolean mStartAnimator = false;
    private int startHeight;
    private int endHeigit;

    public float getScanLineHeight() {
        return scanLineHeight;
    }

    public void setScanLineHeight(int mScanLineHeight) {
        this.scanLineHeight = mScanLineHeight;
        invalidate();
    }

    private void animator() {
        if (!mStartAnimator) {
            return;
        }
        this.clearAnimation();
        animator = ObjectAnimator.ofInt(this, "scanLineHeight", startHeight, endHeigit);
        animator.setDuration(1800);
        animator.addListener(new Animator.AnimatorListener() {
            @Override
            public void onAnimationStart(Animator animation) {
            }

            @Override
            public void onAnimationEnd(Animator animation) {
                if (mStartAnimator) {
                    int temp = startHeight;
                    startHeight = endHeigit;
                    endHeigit = temp;
                    animator();
                }
            }

            @Override
            public void onAnimationCancel(Animator animation) {
            }

            @Override
            public void onAnimationRepeat(Animator animation) {
            }
        });
        animator.start();
    }

    public void startAnimator() {
        if (mStartAnimator) {//如果动画正在运行，不再重复启动
            return;
        }
        mStartAnimator = true;
        int radio = Math.min((getWidth() / 2), getHeight() / 2) - DensityUtil.dip2px(20);
        startHeight = Math.max(DensityUtil.dip2px(20), getHeight() / 2 - radio);
        endHeigit = (getHeight() / 2) + radio;
//        startHeight = DensityUtil.dip2px(20);
//        endHeigit = getHeight() - DensityUtil.dip2px(20);

//        ObjectAnimator animator = ObjectAnimator.ofFloat(this, "scanLineHeight", startHeight, endHeigit);
//        animator.setDuration(1000);
//        animator.start();
        animator();
    }

    public void stopAnimator() {
        mStartAnimator = false;
        if (animator != null) {
            animator.cancel();
            animator = null;
        }
        invalidate();
    }

    public void hideScanLine() {
    }

}
